package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.penpal.bean.MemberDetails;
import com.rongshu.view.penpal.view.PenpalMemberManagerListener;
import com.rongshu.view.penpal.widget.MemberOptionView;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class PenpalMemberListAdapter extends RecyclerView.Adapter<PenpalMemberListAdapter.ViewHolder> {

    private Context context;
    private List<MemberDetails> list;
    private int isEdit;//0 ：查看；1 ：操作
    private PenpalMemberManagerListener listener;
    private AdapterListener headClickListener;
    private int currentRank;
    private int managerCount;
    private int ordinaryCount;
    private int userId;

    public PenpalMemberListAdapter(Context context, List<MemberDetails> list, int isEdit) {
        this.list = list;
        this.context = context;
        this.isEdit = isEdit;
        userId = UserInfoUtils.getUserId();
    }

    public void setData(int currentRank, int managerCount, int ordinaryCount) {
        this.currentRank = currentRank;
        this.managerCount = managerCount;
        this.ordinaryCount = ordinaryCount;
    }

    public void setAdapterListener(PenpalMemberManagerListener listener) {
        this.listener = listener;
    }

    public void setOnHeadClickListener(AdapterListener headClickListener) {
        this.headClickListener = headClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_penpal_member_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
        viewHolder.name.setText(list.get(i).getBi_name());
        if (isEdit == 0 || list.get(i).getRank() == 1 || list.get(i).getUser_id() == UserInfoUtils.getUserId()) {
            viewHolder.downIcon.setVisibility(View.GONE);
        } else {
            viewHolder.downIcon.setVisibility(View.VISIBLE);
        }
        if (i == 0) {
            viewHolder.tag.setVisibility(View.VISIBLE);
            viewHolder.tag.setText("管理层 (" + managerCount + "/3)");
        } else if (list.get(i).getShowTag()) {
            viewHolder.tag.setVisibility(View.VISIBLE);
            viewHolder.tag.setText("其他成员 (" + ordinaryCount + ")");
        } else {
            viewHolder.tag.setVisibility(View.GONE);
        }
        switch (list.get(i).getRank()) {
            case 1:
                viewHolder.level.setText("会长");
                viewHolder.level.setVisibility(View.VISIBLE);
                break;
            case 2:
                viewHolder.level.setText("副会长");
                viewHolder.level.setVisibility(View.VISIBLE);
                break;
            case 3:
                viewHolder.level.setVisibility(View.GONE);
                break;
        }
        if (list.get(i).getIsExpand()) {
            rotateView(viewHolder.downIcon, 90f);
            viewHolder.bottomOption.setVisibility(View.VISIBLE);
        } else {
            rotateView(viewHolder.downIcon, 0f);
            viewHolder.bottomOption.setVisibility(View.GONE);
        }
        viewHolder.bottomOption.showViewByRank(currentRank, list.get(i).getRank());
        viewHolder.bottomOption.setClickListener(listener, list.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (headClickListener != null) {
                    headClickListener.callBack(list.get(i));
                }

            }
        });
        viewHolder.rightOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentRank == 1 || currentRank == 2) {//当前操作用户级别为：会长、副会长
                    if (list.get(i).getRank() != 1 && list.get(i).getUser_id() != userId) {//点击的用户item不为：会长、本人
                        for (int index = 0; index < list.size(); index++) {
                            if (index == i) {
                                list.get(index).setIsExpand(!list.get(index).getIsExpand());
                            } else {
                                list.get(index).setIsExpand(false);
                            }
                        }
                        notifyDataSetChanged();
                    }
                }
            }
        });
    }

    private void rotateView(View view, float degress) {
        Animation anim = new RotateAnimation(0f, degress,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true); // 设置保持动画最后的状态
        anim.setDuration(200); // 设置动画时间
        anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
        view.startAnimation(anim);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, level, tag;
        private ImageView logo, downIcon;
        private LinearLayout rightOption;
        private View bottomLine;
        private MemberOptionView bottomOption;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tag = itemView.findViewById(R.id.top_name);
            level = itemView.findViewById(R.id.member_level);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            downIcon = itemView.findViewById(R.id.down_icon);
            rightOption = itemView.findViewById(R.id.right_option);
            bottomLine = itemView.findViewById(R.id.bottom_line);
            bottomOption = itemView.findViewById(R.id.bottom_option);
        }
    }
}
